<!DOCTYPE HTML>
<html lang="cn" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <%include("/common/header.html",{title:''}){}%>
    <title>商品列表</title>
    <link rel="stylesheet" href="css/formSelects-v4.css" />
</head>
<body>

<div class="x-nav" style="margin: 20px auto auto 15px;">
      <span class="layui-breadcrumb" lay-filter="breadcrumb" style="visibility: visible;">
        <a href="javascript:;">首页</a><span lay-separator="">/</span>
        <a href="javascript:;">商品管理</a><span lay-separator="">/</span>
        <a><cite>优惠券列表</cite></a>
      </span>
    <a class="layui-btn layui-btn-primary layui-btn-small" style="margin: -5px 14px 0px 0px;float:right;"
       href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon layui-icon-refresh-3"></i>
    </a>
</div>



<div class="layui-form layui-card-header layuiadmin-card-header-auto">
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">开始时间:</label>
            <div class="layui-input-block">
                <input type="text" class="layui-input" id="test1" name="beginTime" placeholder="请选择开始时间">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">名称:</label>
            <div class="layui-input-block">
                <input type="text" name="couponName" lay-verify="required" value="" class="layui-input"
                       placeholder="请输入优惠券名称">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">周期:</label>
            <div class="layui-input-block">
                <select name="quiz3">
                    <option value="">请选择</option>
                    <option value="0">天</option>
                    <option value="1">月</option>
                    <option value="2">年</option>
                    <option value="3">永久</option>
                </select>
            </div>
        </div>
        <div class="layui-inline">
            <button class="layui-btn layuiadmin-btn-useradmin" lay-submit lay-filter="LAY-user-front-search">
                <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
            </button>
        </div>
    </div>
</div>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <table id="LAY-user-manage" lay-filter="LAY-user-manage"></table>
            <script type="text/html" id="toolbarDemo">
                <button class="layui-btn layuiadmin-btn-useradmin" lay-event="add">新增优惠券</button>
                <!--<button class="layui-btn layuiadmin-btn-useradmin" lay-event="combination">组合商品</button>-->
            </script>
            <script type="text/html" id="imgTpl">
                <img style="display: inline-block; width: 50%; height: 100%;" src={{ d.avatar }}>
            </script>
            <script type="text/html" id="table-useradmin-webuser">
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i
                        class="layui-icon layui-icon-edit"></i>编辑</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i
                        class="layui-icon layui-icon-delete"></i>删除</a>
            </script>

            <script type="text/html" id="table-useradmin-webuser-b">
                <a class="layui-btn  layui-btn-xs" lay-event="viewGood"><i class="layui-icon layui-icon-form"></i>查看</a>
            </script>
        </div>
    </div>
</div>
<!-- 单个商品弹出页面 -->
<div class="layui-form" lay-filter="layuiadmin-form-useradmin"
     id="layuiadmin-form-useradmin"
     style="padding: 15px;display: none">
    <form action class="layui-form" lay-filter="component-form-elements" id="myform">
        <input type="hidden" name="id" id="id">
        <div class="layui-form-item">
            <label class="layui-form-label" style="padding-left: 0px;font-size: medium;width: auto;">名称:</label>
            <div class="layui-input-inline">
                <input type="text" name="couponName" lay-verify="required" value="" class="layui-input"
                       placeholder="请输入优惠券名称">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label" style="padding-left: 0px;font-size: medium;width: auto;">类型:</label>
            <div class="layui-input-inline">
                <select name="typeName">
                    <option value="">请选择</option>
                    <option value="0">折扣券</option>
                    <option value="1">代金券</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label" style="padding-left: 0px;font-size: medium;width: auto;">开始时间:</label>
                <div class="layui-input-inline">
                    <input type="text" class="layui-input" id="test2" name="beginTime" placeholder="请选择开始时间">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label" style="padding-left: 0px;font-size: medium;width: auto;">周期:</label>
            <div class="layui-input-inline">
                <select name="time">
                    <option value="">请选择</option>
                    <option value="0">天</option>
                    <option value="1">月</option>
                    <option value="2">年</option>
                    <option value="3">永久</option>
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label" style="padding-left: 0px;font-size: medium;width: auto;">针对产品:</label>
            <div class="layui-input-block" >
                <input type="checkbox" lay-filter="allChoose"  title="全选">
                <div class="search-box" id="good">

                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit="" lay-filter="component-form-elements">立即提交</button>
            </div>
        </div>
    </form>
</div>

<div class="layui-form" lay-filter="layuiadmin-form-useradmin-a"
     id="layuiadmin-form-useradmin-b"
     style="padding: 15px;display: none">
    <form class="layui-form" action="" lay-filter="component-form-element-b" id="myform1">
        <input type="hidden" name="id">
        <div class="layui-form-item">
            <div class="layui-input-inline">

            </div>
        </div>
    </form>
</div>





<script id="demo1" type="text/html">
    {{#  layui.each(d.list1, function(index, item1){ }}
    <input type="checkbox" name="goods_id" value="{{ item1.id }}" title="{{ item1.goodName }}">
    {{#  }); }}
    {{#  if(d.list1.length === 0){ }}
    <div style="color: red;margin-top: 10px;">暂无商品数据</div>
    {{#  } }}
</script>
<!--判断优惠券类型-->
<script id="demo2" type="text/html">
    {{#  if(d.typeName=='0'){ }}
    <span>折扣券</span>
    {{#  } }}
    {{#  if(d.typeName=='1'){ }}
    <span>代金券</span>
    {{#  } }}
</script>

<!--周期-->
<script id="demo3" type="text/html">
    {{#  if(d.time=='0'){ }}
    <span>天</span>
    {{#  } }}
    {{#  if(d.time=='1'){ }}
    <span>月</span>
    {{#  } }}
    {{#  if(d.time=='2'){ }}
    <span>年</span>
    {{#  } }}
    {{#  if(d.time=='3'){ }}
    <span>永久</span>
    {{#  } }}
</script>

<%include("/common/foot.html"){}%>
<script src="http://libs.baidu.com/jquery/1.9.1/jquery.min.js"></script>
<script src="js/formSelects-v4.js" type="text/javascript" charset="utf-8"></script>
<script>

    layui.use(['index', 'table', 'form', 'laydate', 'layedit', 'AjaxCommon', 'AliangLayui'], function () {
        var $ = layui.$;
        var table = layui.table;
        var form = layui.form;
        var laydate = layui.laydate;
        var upload = layui.upload;
        var layedit = layui.layedit;
        var AliangLayui = layui.AliangLayui;
        var index;


        //富文本编辑器
        layedit = layui.layedit;

        var cols = [ //表头
            {checkbox: true, fixed: true},
            {field: 'couponName',  title: '名称'},
            {field: 'typeName',  title: '类型',templet:"#demo2"},
            {field: 'time',  title: '周期',templet: "#demo3"},
            {field:'goods_id',title:'针对商品',toolbar: '#table-useradmin-webuser-b'},
            {field: 'beginTime',  title: '开始时间'},
            {title: '操作',  toolbar: '#table-useradmin-webuser', fixed: 'right'}
        ];

        form.on('checkbox(allChoose)', function (data) {
            var child = $(".search-box input[type='checkbox']");
            child.each(function (index, item) {
                item.checked = data.elem.checked;
            });
            form.render('checkbox');
        });

        laydate.render({
            elem: '#test1'
        });
        laydate.render({
            elem: '#test2'
        });

        var title = "优惠券信息";
        var size = ['100%', '100%']
        var findAll_url = 'admin/coupon/findAll.jhtml';
        var update_url = 'admin/coupon/saveCoupon.jhtml';
        var delete_url = 'admin/coupon/delete.jhtml';
        var findAllGoods_url="goods/list/findAll.jhtml";


        AliangLayui.createTable(
            "#LAY-user-manage",
            "listReload",
            "#toolbarDemo",
            title,
            findAll_url,
            true,
            [10, 15, 20, 25],
            ['filter', 'print', 'exports'],
            cols,
            null
        );

        //自定义验证规则
        form.verify({
            required: [/[\S]+/, "必填项不能为空"],
            image: function (value) {
                if (value == "") {
                    return '您还未上传图片信息';
                }
            }

        });

        //时间范围
        laydate.render({
            elem: '#test9'
            , type: 'time'
            , range: true
        });

        table.on('tool(LAY-user-manage)', function (obj) {
            var data = obj.data;
            if (obj.event === 'detail') {
                layer.msg('ID：' + data.id + ' 的查看操作');
            } else if (obj.event === 'edit') {
                initUpdate(data);
            }else if(obj.event=='viewGood'){
                openViewGoods(data.id);
            } else if (obj.event === 'del') {
                layer.confirm('删除以后数据将不能恢复，确定删除吗？', function (index) {
                    //alert(data.id);
                    postDelete(data.id);
                });
            }
        });

        function openViewGoods(data){
            layer.open({
                type: 1,
                title: "针对产品",
                area: ['100%', '100%'],
                content: $('#layuiadmin-form-useradmin-b')
            });
        }

        //头工具栏事件
        table.on('toolbar(LAY-user-manage)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'add':
                    initUpdate(0);
                    break;
                case 'combination':
                    //initUpdates(0);
                    break;
                case 'export':
                    layer.msg("导出");
                    break;
            }
        });

        form.on('submit(LAY-user-front-search)', function (data) {
            var field = data.field;
            table.reload('listReload', {
                where: {
                    name: field.name,
                    price: field.price,
                    pay: field.pay
                },
                page: {
                    curr: 1
                }
            });
        });


        //初始化日期模块
        laydate.render({
            elem: '#create' //指定元素
        });
        //弹出修改页面调用初始化数据方法
        function initUpdate(data) {
            $('#myform')[0].reset();
            layer.open({
                title: title,
                area: size,
                type: 1,
                content: $('#layuiadmin-form-useradmin'), //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                btn: ['关闭'], //可以无限个按钮
                cancel: function (index, layero) {//取消事件
                    location.replace(location.href);　//layer.open关闭刷新
                },
            });
            $.ajax({
                url: findAllGoods_url,
                type: "POST",
                data: {
                },
                dataType: "json",
                success: function (ret) {
                    //alert(JSON.stringify(ret.data))
                    var laytpl = layui.laytpl;
                    var ret2 = { //数据
                        "list1": ret.data
                    };
                    laytpl($("#demo1").html()).render(ret2, function (html) {
                        $("#good").html(html);
                    });
                    form.render();
                },
                error: function (jqXHR, textStatus, errorThrown) {
                }
            });

            // layui.form.val("component-form-element", loadData(data));

        }

        $.fn.ghostsf_serialize = function () {
            var a = this.serializeArray();
            var $radio = $('input[type=checkbox]', this);
            var temp = {};
            $.each($radio, function () {
                if (!temp.hasOwnProperty(this.name)) {
                    if ($("input[name='" + this.name + "']:checked").length == 0) {
                        temp[this.name] = "";
                        a.push({name: this.name, value: ""});
                    }
                }
            });
            return a;
        };

        //修改页面的内容提交到后台处理
        form.on('submit(component-form-elements)', function (data) {
            data.field.content = layedit.getContent(index);
            var formObject={};
            var formArray=$("#myform").ghostsf_serialize();
            $.each(formArray,function(i,item){
                formObject[item.name]=item.value;
            })
            var checkbox =[];
            $("input[name=goods_id]:checked").each(function(index,item){
                checkbox.push(item.value)
            });
            formObject["goods_id"]=checkbox.toString();
            postUpdate(formObject);
            return false;
        });


        //ajax提交删除内容
        window.postDelete = function (id) {
            $.ajax({
                url: delete_url,
                type: "POST",
                data: {
                    "id": id
                },
                dataType: "json",
                success: function (ret) {
                    if (ret == null) {
                        layer.msg("删除失败", {
                            icon: 5
                        });
                    } else {
                        layer.msg("删除成功", {
                            icon: 6
                        });
                        layer.closeAll();
                        table.reload('listReload');
                    }
                    return ret;
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    var estr = "Page:" + window.location.pathname + ",Function: postDelete()";
                    layer.open({
                        title: "错误信息",
                        area: ['100%', '100%'],
                        type: 2,
                        content: "base/error.jhtml?info=" + estr, //
                        btn: ['关闭'] //可以无限个按钮
                    });
                }
            });
        }

        //ajax提交修改内容
        window.postUpdate = function (data) {
            // alert(JSON.stringify(data))
            var formData=new FormData()
            formData.append("data",JSON.stringify(data));
            $.ajax({
                url: update_url,
                type: "POST",
                //traditional:true,
                data: formData,
                processData: false,
                contentType: false,
                success: function (data) {
                    if (data.code == 0) {
                        layer.closeAll();
                        layer.msg(data.msg, {time: 600, icon: 1}, function () {
                            //table.reload('listReload');

                        });
                        location.replace(location.href);
                    } else {
                        layer.msg(data.msg, {icon: 6});
                    }
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    var estr = "Page:" + window.location.pathname + ",Function:postUpdate()";
                    layer.open({
                        title: "错误信息",
                        type: 2,
                        content: "base/error.jhtml?info=" + estr,
                        btn: ['关闭'] //可以无限个按钮
                    });
                }
            });
        }
    });

</script>
</body>

</html>